//
//  RunHomeVC.swift
//  BUYOU
//
//  Created by tyuu on 2024/5/29.
//

import UIKit
import SwiftyJSON
import HandyJSON
import JXSegmentedView
class RunHomeVC: BaseViewController {
    
    var json: JSON?

    var datas: [RunActivity] = []
    
    override func viewDidLoad() {
        super.viewDidLoad()

        // Do any additional setup after loading the view.
        
        navigationItem.title = "步友活动"
        
        loadData()
        
        
        tableView.dataSource = self
        tableView.delegate = self
        tableView.register(cellWithClass: CustomTableViewCell.self)
        view.addSubview(tableView)
        tableView.snp.makeConstraints { make in
            make.edges.equalToSuperview()
        }
        
        
    }
    
    func loadData() {
        
        let postUrl = URL(string: "https://ddcczz11dd.typicode.com/posts")!
        let postBody = """
        {
            "title": "foo",
            "body": "bar",
            "userId": 1
        }
        """.data(using: .utf8)

        NetworkManager.shared.post(url: postUrl, body: postBody) { result in
            switch result {
            case .success(let data):
                print("POST 请求成功，数据：\(String(data: data, encoding: .utf8) ?? "无数据")")
            case .failure(let error):
                print("POST 请求失败，错误：\(error.localizedDescription)")
            }
            
            self.json = JSON(RunActivity.jsonString)
            let arr = self.json?.arrayValue.map({
                return $0.stringValue
            }).compactMap({ return $0 }) ?? []
            
            self.datas = [RunActivity].deserialize(from: self.json?.stringValue, designatedPath: "")?.compactMap({ return $0 }) ?? []
            print(self.json?.stringValue)
            self.tableView.reloadData()
            
        }

        
    }

    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        // Get the new view controller using segue.destination.
        // Pass the selected object to the new view controller.
    }
    */

}

extension RunHomeVC: UITableViewDataSource, UITableViewDelegate {
    
    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
        let cell = tableView.dequeueReusableCell(withClass: CustomTableViewCell.self)
        cell.configure(with: datas[indexPath.row])
        return cell
    }
    
    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
        return datas.count
    }
    
    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
        let vc = ActivityDetailViewController(activity: datas[indexPath.row])
        self.navigationController?.pushViewController(vc)
    }
    
}
extension RunHomeVC: JXSegmentedListContainerViewListDelegate {
    func listView() -> UIView {
        return view
    }
}
